<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accountType">
              <j-dict-select-tag type="radio" v-model="model.accountType" dictCode="account_type" @input="switchAccountType" placeholder="请选择类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入余额" style="width: 100%"/>
            </a-form-model-item>
          </a-col>


          <a-col :span="24" v-show="statusFilter(['2','3','4','5','6','10','20'])">
            <a-form-model-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accountNumber">
              <a-input v-model="model.accountNumber" placeholder="请输入账号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['10','20'])">
            <a-form-model-item label="开卡时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择开卡时间" v-model="model.startDate" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['10','20'])">
            <a-form-model-item label="到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endData">
              <j-date placeholder="请选择到期时间" v-model="model.endData" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload isMultiple v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['10','20'])">
            <a-form-model-item label="持卡人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userName">
              <a-input v-model="model.userName" placeholder="请输入持卡人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['20'])">
            <a-form-model-item label="安全码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="securityCode">
              <a-input v-model="model.securityCode" placeholder="请输入安全码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['6','20'])">
            <a-form-model-item label="密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="passwd">
              <a-input v-model="model.passwd" placeholder="请输入密码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['6','10','20'])">
            <a-form-model-item label="绑定手机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bindPhone">
              <a-input v-model="model.bindPhone" placeholder="请输入绑定手机"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['6','20'])">
            <a-form-model-item label="出账日" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payOutDay">
              <j-date placeholder="请选择出账日" v-model="model.payOutDay" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="statusFilter(['6','20'])">
            <a-form-model-item label="还款日" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repaymenDay">
              <j-date placeholder="请选择还款日" v-model="model.repaymenDay" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'

export default {
  name: 'FinanceAccountForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  // filters: {
  //   statusFilter(status) {
  //     console.log(status)
  //     return true
  //   },
  // },
  data() {
    return {
      model: {},
      labelCol: {xs: {span: 24}, sm: {span: 5},},
      wrapperCol: {xs: {span: 24}, sm: {span: 16},},
      confirmLoading: false,
      validatorRules: {
        accountType:[ { required: true, message: '请选择类型!' },]
      },
      url: {
        add: "/finance/financeAccount/add",
        edit: "/finance/financeAccount/edit",
        queryById: "/finance/financeAccount/queryById"
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    switchAccountType(e) {
      this.switchAccount = e;
    },
    statusFilter(v) {
      console.log('>>',this.switchAccount,v,v.includes(this.switchAccount))
      return v.includes(this.switchAccount)
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>